from django.db import models

# Create your models here.

class College(models.Model):
    """
    学院表
    @description 存储学院基本信息
    @created 2024-05-19
    """
    id = models.AutoField(primary_key=True, verbose_name='学院ID')
    name = models.CharField(max_length=100, null=True, blank=True, verbose_name='学院名称')
    # 根据数据库结构，移除不存在的字段
    description = models.TextField(null=True, blank=True, verbose_name='学院描述')
    icon = models.CharField(max_length=20, null=True, blank=True, verbose_name='学院图标')
    
    class Meta:
        db_table = 'colleges'  # 指定表名
        verbose_name = '学院'
        verbose_name_plural = '学院'
        
    def __str__(self):
        return self.name


class Grade(models.Model):
    """
    成绩表
    @description 存储学生成绩信息
    @created 2024-05-19
    """
    grade_id = models.AutoField(primary_key=True, verbose_name='成绩ID')
    student_id = models.IntegerField(null=True, blank=True, verbose_name='学生ID')
    course_id = models.IntegerField(null=True, blank=True, verbose_name='课程ID')
    final_grade = models.DecimalField(max_digits=5, decimal_places=2, null=True, verbose_name='最终成绩')
    
    class Meta:
        db_table = 'grades'  # 指定表名
        verbose_name = '成绩'
        verbose_name_plural = '成绩'
        
    def __str__(self):
        return f'学生{self.student_id}的课程{self.course_id}成绩'


class Course(models.Model):
    """
    课程表
    @description 存储课程信息
    @created 2024-07-10
    """
    course_id = models.AutoField(primary_key=True, verbose_name='课程ID')
    name = models.CharField(max_length=100, null=True, blank=True, verbose_name='课程名称')
    code = models.CharField(max_length=50, null=True, blank=True, verbose_name='课程代码')
    description = models.TextField(null=True, blank=True, verbose_name='课程描述')
    credit = models.DecimalField(max_digits=3, decimal_places=1, null=True, blank=True, verbose_name='学分')
    hours = models.IntegerField(null=True, blank=True, verbose_name='课时')
    college_id = models.IntegerField(null=True, blank=True, verbose_name='所属学院ID')
    status = models.SmallIntegerField(null=True, blank=True, default=1, verbose_name='状态')
    
    class Meta:
        db_table = 'courses'  # 指定表名
        verbose_name = '课程'
        verbose_name_plural = '课程'
        indexes = [
            models.Index(fields=['name'], name='idx_course_name'),
            models.Index(fields=['college_id'], name='idx_course_college'),
        ]
        
    def __str__(self):
        return self.name


class SchoolClass(models.Model):
    """
    班级表
    @description 存储班级信息
    @created 2024-07-10
    """
    id = models.AutoField(primary_key=True, verbose_name='班级ID')
    name = models.CharField(max_length=100, null=True, blank=True, verbose_name='班级名称')
    grade = models.CharField(max_length=20, null=True, blank=True, verbose_name='年级')
    college = models.ForeignKey(College, on_delete=models.SET_NULL, null=True, blank=True, verbose_name='所属学院', db_column='college_id', related_name='school_classes')
    department = models.CharField(max_length=100, null=True, blank=True, verbose_name='所属系部')
    status = models.SmallIntegerField(null=True, blank=True, default=1, verbose_name='状态')
    
    class Meta:
        db_table = 'school_classes'  # 指定表名
        verbose_name = '班级'
        verbose_name_plural = '班级'
        indexes = [
            models.Index(fields=['name'], name='idx_class_name'),
            models.Index(fields=['college'], name='idx_class_college'),
        ]
        
    def __str__(self):
        return self.name


class StudentClass(models.Model):
    """
    学生班级关联表
    @description 存储学生与班级的多对多关系
    @created 2024-07-10
    """
    id = models.AutoField(primary_key=True)
    student_id = models.IntegerField(null=True, blank=True, verbose_name='学生ID')
    class_id = models.IntegerField(null=True, blank=True, verbose_name='班级ID')
    
    class Meta:
        db_table = 'student_class'  # 指定表名
        verbose_name = '学生班级关联'
        verbose_name_plural = '学生班级关联'
        unique_together = ('student_id', 'class_id')  # 组合唯一约束
        indexes = [
            models.Index(fields=['student_id'], name='idx_student_class_student'),
            models.Index(fields=['class_id'], name='idx_student_class_class'),
        ]
        
    def __str__(self):
        return f'学生{self.student_id}-班级{self.class_id}'


class CourseSchedule(models.Model):
    """
    课程安排表
    @description 存储课程的上课时间和地点
    @created 2024-07-10
    """
    id = models.AutoField(primary_key=True)
    course_id = models.IntegerField(null=True, blank=True, verbose_name='课程ID')
    teacher_id = models.IntegerField(null=True, blank=True, verbose_name='教师ID')
    day_of_week = models.SmallIntegerField(null=True, blank=True, verbose_name='星期几')  # 0-6，对应周一到周日
    start_time = models.SmallIntegerField(null=True, blank=True, verbose_name='开始节次')
    end_time = models.SmallIntegerField(null=True, blank=True, verbose_name='结束节次')
    location = models.CharField(max_length=100, null=True, blank=True, verbose_name='上课地点')
    semester = models.CharField(max_length=20, null=True, blank=True, verbose_name='学期')
    
    class Meta:
        db_table = 'course_schedules'  # 指定表名
        verbose_name = '课程安排'
        verbose_name_plural = '课程安排'
        indexes = [
            models.Index(fields=['course_id'], name='idx_schedule_course'),
            models.Index(fields=['teacher_id'], name='idx_schedule_teacher'),
        ]
        
    def __str__(self):
        day_map = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
        day = day_map[self.day_of_week] if 0 <= self.day_of_week < len(day_map) else "未知"
        return f'课程{self.course_id}-{day}{self.start_time}-{self.end_time}节'


class Student(models.Model):
    """
    学生表
    @description 存储学生基本信息
    @created 2024-05-19
    """
    student_id = models.AutoField(primary_key=True, verbose_name='学生ID')
    name = models.CharField(max_length=255, null=True, blank=True, verbose_name='学生姓名')
    email = models.CharField(max_length=255, null=True, blank=True, unique=True, verbose_name='邮箱')
    enrollment_date = models.DateField(null=True, blank=True, verbose_name='入学日期')
    
    class Meta:
        db_table = 'students'  # 指定表名
        verbose_name = '学生'
        verbose_name_plural = '学生'
        
    def __str__(self):
        return self.name


class Teacher(models.Model):
    """
    教师表
    @description 存储教师信息，包括基本信息、职称、学历等
    @created 2024-04-08
    """
    teacher_id = models.AutoField(primary_key=True, verbose_name='教师ID')
    user_id = models.IntegerField(unique=True, null=True, blank=True, verbose_name='关联用户ID')
    college_id = models.IntegerField(null=True, blank=True, verbose_name='所属学院ID')
    teacher_name = models.CharField(max_length=50, null=True, blank=True, verbose_name='教师姓名')
    teacher_code = models.CharField(max_length=50, null=True, blank=True, unique=True, verbose_name='教师工号')
    title = models.CharField(max_length=50, null=True, blank=True, verbose_name='职称')
    department = models.CharField(max_length=100, null=True, blank=True, verbose_name='所属系部')
    education = models.CharField(max_length=50, null=True, blank=True, verbose_name='学历')
    specialty = models.CharField(max_length=100, null=True, blank=True, verbose_name='专业方向')
    entry_date = models.DateField(null=True, blank=True, verbose_name='入职日期')
    status = models.SmallIntegerField(null=True, blank=True, default=1, verbose_name='状态')
    
    class Meta:
        db_table = 'teacher'  # 指定表名
        verbose_name = '教师'
        verbose_name_plural = '教师'
        
    def __str__(self):
        return self.teacher_name


class TeachingRecord(models.Model):
    """
    授课记录表
    @description 记录教师授课信息，包括课程、班级、学期和授课时间等
    @created 2024-04-08
    """
    record_id = models.AutoField(primary_key=True, verbose_name='记录ID')
    teacher_id = models.IntegerField(null=True, blank=True, verbose_name='教师ID')
    course_id = models.IntegerField(null=True, blank=True, verbose_name='课程ID')
    class_id = models.IntegerField(null=True, blank=True, verbose_name='班级ID')
    semester_id = models.IntegerField(null=True, blank=True, verbose_name='学期ID')
    start_date = models.DateField(null=True, blank=True, verbose_name='开课日期')
    end_date = models.DateField(null=True, blank=True, verbose_name='结课日期')
    status = models.SmallIntegerField(null=True, blank=True, default=1, verbose_name='状态')
    
    class Meta:
        db_table = 'teaching_record'  # 指定表名
        verbose_name = '授课记录'
        verbose_name_plural = '授课记录'
        indexes = [
            models.Index(fields=['teacher_id'], name='idx_teaching_teacher'),
            models.Index(fields=['course_id'], name='idx_teaching_course'),
            models.Index(fields=['class_id'], name='idx_teaching_class'),
            models.Index(fields=['semester_id'], name='idx_teaching_semester'),
        ]
        
    def __str__(self):
        return f'授课记录-{self.record_id}'


class Resource(models.Model):
    """
    资源表
    @description 存储教学资源信息，包括文档、视频、音频等各类资源
    @created 2024-04-08
    """
    resource_id = models.AutoField(primary_key=True, verbose_name='资源ID')
    category_id = models.IntegerField(null=True, blank=True, verbose_name='分类ID')
    creator_id = models.IntegerField(null=True, blank=True, verbose_name='创建人ID')
    resource_name = models.CharField(max_length=100, null=True, blank=True, verbose_name='资源名称')
    resource_type = models.CharField(max_length=50, null=True, blank=True, verbose_name='资源类型')
    file_path = models.CharField(max_length=255, null=True, blank=True, verbose_name='文件路径')
    file_size = models.IntegerField(null=True, blank=True, verbose_name='文件大小(KB)')
    description = models.CharField(max_length=500, null=True, blank=True, verbose_name='资源描述')
    audit_status = models.SmallIntegerField(null=True, blank=True, default=0, verbose_name='审核状态')
    audit_opinion = models.CharField(max_length=200, null=True, blank=True, verbose_name='审核意见')
    download_count = models.IntegerField(null=True, blank=True, default=0, verbose_name='下载次数')
    create_time = models.DateTimeField(null=True, blank=True, verbose_name='创建时间')
    update_time = models.DateTimeField(null=True, blank=True, verbose_name='更新时间')
    status = models.SmallIntegerField(null=True, blank=True, default=1, verbose_name='状态')
    dify_doc_id = models.CharField(max_length=100, null=True, blank=True, verbose_name='Dify知识库文档ID')
    ai_tags = models.TextField(null=True, blank=True, verbose_name='AI标签(JSON)')
    ai_score = models.FloatField(null=True, blank=True, verbose_name='AI评分')
    ai_category = models.CharField(max_length=100, null=True, blank=True, verbose_name='AI主分类')
    ai_subcategory = models.CharField(max_length=100, null=True, blank=True, verbose_name='AI子分类')
    ai_subject_area = models.CharField(max_length=100, null=True, blank=True, verbose_name='AI学科领域')
    ai_value = models.TextField(null=True, blank=True, verbose_name='AI教学价值')
    ai_recommend_uses = models.TextField(null=True, blank=True, verbose_name='AI推荐用途(JSON)')
    
    class Meta:
        db_table = 'resource'  # 指定表名
        verbose_name = '资源'
        verbose_name_plural = '资源'
        indexes = [
            models.Index(fields=['category_id'], name='idx_resource_category'),
            models.Index(fields=['creator_id'], name='idx_resource_creator'),
            models.Index(fields=['resource_type'], name='idx_resource_type'),
            models.Index(fields=['audit_status'], name='idx_resource_audit'),
        ]
        
    def __str__(self):
        return self.resource_name


# 一、AI助手管理相关表

class AIAssistant(models.Model):
    """
    AI助手表
    @description 存储AI助手信息，包括基本信息、知识领域和个性化指令等
    @created 2024-04-08
    """
    assistant_id = models.AutoField(primary_key=True, verbose_name='助手ID')
    name = models.CharField(max_length=100, null=True, blank=True, verbose_name='助手名称')
    description = models.CharField(max_length=500, null=True, blank=True, verbose_name='助手描述')
    teacher_id = models.IntegerField(null=True, blank=True, verbose_name='教师ID')
    subject = models.CharField(max_length=100, null=True, blank=True, verbose_name='知识领域')
    custom_instructions = models.TextField(null=True, blank=True, verbose_name='个性化指令')
    is_active = models.SmallIntegerField(null=True, blank=True, default=1, verbose_name='状态')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'ai_assistants'  # 指定表名
        verbose_name = 'AI助手'
        verbose_name_plural = 'AI助手'
        indexes = [
            models.Index(fields=['teacher_id'], name='idx_assistant_teacher'),
        ]
        
    def __str__(self):
        return self.name


class AssistantCourse(models.Model):
    """
    AI助手关联课程表
    @description 关联AI助手与课程的多对多关系
    @created 2024-04-08
    """
    id = models.AutoField(primary_key=True)
    assistant_id = models.IntegerField(null=True, blank=True, verbose_name='助手ID')
    course_id = models.IntegerField(null=True, blank=True, verbose_name='课程ID')
    
    class Meta:
        db_table = 'assistant_courses'  # 指定表名
        verbose_name = 'AI助手课程关联'
        verbose_name_plural = 'AI助手课程关联'
        unique_together = ('assistant_id', 'course_id')  # 组合唯一约束
        indexes = [
            models.Index(fields=['assistant_id'], name='idx_ac_assistant'),
            models.Index(fields=['course_id'], name='idx_ac_course'),
        ]
        
    def __str__(self):
        return f'助手{self.assistant_id}-课程{self.course_id}'


class AssistantConversation(models.Model):
    """
    AI助手对话记录表
    @description 记录用户与AI助手的对话内容
    @created 2024-04-08
    """
    conversation_id = models.AutoField(primary_key=True, verbose_name='对话ID')
    assistant_id = models.IntegerField(null=True, blank=True, verbose_name='助手ID')
    user_id = models.IntegerField(null=True, blank=True, verbose_name='用户ID')
    question = models.TextField(null=True, blank=True, verbose_name='问题内容')
    answer = models.TextField(null=True, blank=True, verbose_name='回答内容')
    response_time = models.IntegerField(null=True, blank=True, verbose_name='响应时间(毫秒)')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        db_table = 'assistant_conversations'  # 指定表名
        verbose_name = 'AI助手对话'
        verbose_name_plural = 'AI助手对话'
        indexes = [
            models.Index(fields=['assistant_id'], name='idx_conv_assistant'),
            models.Index(fields=['user_id'], name='idx_conv_user'),
            models.Index(fields=['created_at'], name='idx_conv_time'),
        ]
        
    def __str__(self):
        return f'对话{self.conversation_id}'


class AssistantAnalytics(models.Model):
    """
    AI助手使用统计表
    @description 统计AI助手的使用情况，包括对话次数和用户数等
    @created 2024-04-08
    """
    id = models.AutoField(primary_key=True)
    assistant_id = models.IntegerField(null=True, blank=True, verbose_name='助手ID')
    date = models.DateField(null=True, blank=True, verbose_name='日期')
    conversation_count = models.IntegerField(null=True, blank=True, verbose_name='对话次数')
    unique_users = models.IntegerField(null=True, blank=True, verbose_name='独立用户数')
    avg_response_time = models.IntegerField(null=True, blank=True, verbose_name='平均响应时间')
    
    class Meta:
        db_table = 'assistant_analytics'  # 指定表名
        verbose_name = 'AI助手使用统计'
        verbose_name_plural = 'AI助手使用统计'
        unique_together = ('assistant_id', 'date')  # 组合唯一约束
        indexes = [
            models.Index(fields=['assistant_id'], name='idx_analytics_assistant'),
            models.Index(fields=['date'], name='idx_analytics_date'),
        ]
        
    def __str__(self):
        return f'助手{self.assistant_id}-{self.date}'


# 二、AI辅助决策分析系统相关表

class DecisionQuery(models.Model):
    """
    决策查询记录表
    @description 记录用户进行的决策查询及结果
    @created 2024-04-08
    """
    query_id = models.AutoField(primary_key=True, verbose_name='查询ID')
    user_id = models.IntegerField(null=True, blank=True, verbose_name='用户ID')
    query_text = models.TextField(null=True, blank=True, verbose_name='查询文本')
    result = models.TextField(null=True, blank=True, verbose_name='查询结果')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        db_table = 'decision_queries'  # 指定表名
        verbose_name = '决策查询记录'
        verbose_name_plural = '决策查询记录'
        indexes = [
            models.Index(fields=['user_id'], name='idx_query_user'),
            models.Index(fields=['created_at'], name='idx_query_time'),
        ]
        
    def __str__(self):
        return f'查询{self.query_id}'


class DecisionAnomaly(models.Model):
    """
    异常模式识别表
    @description 存储系统识别出的异常模式
    @created 2024-04-08
    """
    anomaly_id = models.AutoField(primary_key=True, verbose_name='异常ID')
    title = models.CharField(max_length=100, null=True, blank=True, verbose_name='异常标题')
    description = models.TextField(null=True, blank=True, verbose_name='异常描述')
    level = models.CharField(max_length=20, null=True, blank=True, verbose_name='严重程度')
    source_data = models.TextField(null=True, blank=True, verbose_name='源数据')
    detected_at = models.DateTimeField(null=True, blank=True, verbose_name='检测时间')
    status = models.SmallIntegerField(null=True, blank=True, default=0, verbose_name='状态')
    
    class Meta:
        db_table = 'decision_anomalies'  # 指定表名
        verbose_name = '异常模式识别'
        verbose_name_plural = '异常模式识别'
        indexes = [
            models.Index(fields=['level'], name='idx_anomaly_level'),
            models.Index(fields=['status'], name='idx_anomaly_status'),
            models.Index(fields=['detected_at'], name='idx_anomaly_time'),
        ]
        
    def __str__(self):
        return self.title


class DecisionRecommendation(models.Model):
    """
    决策建议表
    @description 存储系统生成的决策建议
    @created 2024-04-08
    """
    recommendation_id = models.AutoField(primary_key=True, verbose_name='建议ID')
    title = models.CharField(max_length=100, null=True, blank=True, verbose_name='建议标题')
    content = models.TextField(null=True, blank=True, verbose_name='建议内容')
    category = models.CharField(max_length=50, null=True, blank=True, verbose_name='分类')
    confidence = models.IntegerField(null=True, blank=True, verbose_name='可信度(百分比)')
    is_adopted = models.SmallIntegerField(null=True, blank=True, default=0, verbose_name='是否已采纳')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        db_table = 'decision_recommendations'  # 指定表名
        verbose_name = '决策建议'
        verbose_name_plural = '决策建议'
        indexes = [
            models.Index(fields=['category'], name='idx_recommendation_category'),
            models.Index(fields=['confidence'], name='idx_recommendation_confidence'),
            models.Index(fields=['is_adopted'], name='idx_recommendation_adopted'),
        ]
        
    def __str__(self):
        return self.title


# 三、教学质量智能评估系统相关表

class QualityIndicator(models.Model):
    """
    质量评估指标表
    @description 定义教学质量评估的指标体系
    @created 2024-04-08
    """
    indicator_id = models.AutoField(primary_key=True, verbose_name='指标ID')
    name = models.CharField(max_length=100, null=True, blank=True, verbose_name='指标名称')
    category = models.CharField(max_length=50, null=True, blank=True, verbose_name='指标类别')
    weight = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True, verbose_name='权重')
    description = models.CharField(max_length=500, null=True, blank=True, verbose_name='指标描述')
    
    class Meta:
        db_table = 'quality_indicators'  # 指定表名
        verbose_name = '质量评估指标'
        verbose_name_plural = '质量评估指标'
        indexes = [
            models.Index(fields=['category'], name='idx_indicator_category'),
        ]
        
    def __str__(self):
        return self.name


class TeachingQualityAssessment(models.Model):
    """
    教学质量评估表
    @description 记录教学质量评估的综合结果
    @created 2024-04-08
    """
    assessment_id = models.AutoField(primary_key=True, verbose_name='评估ID')
    college_id = models.IntegerField(null=True, blank=True, verbose_name='学院ID')
    course_id = models.IntegerField(null=True, blank=True, verbose_name='课程ID')
    teacher_id = models.IntegerField(null=True, blank=True, verbose_name='教师ID')
    semester = models.CharField(max_length=20, null=True, blank=True, verbose_name='学期标识')
    overall_score = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True, verbose_name='总体评分')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        db_table = 'teaching_quality_assessments'  # 指定表名
        verbose_name = '教学质量评估'
        verbose_name_plural = '教学质量评估'
        indexes = [
            models.Index(fields=['college_id'], name='idx_assessment_college'),
            models.Index(fields=['course_id'], name='idx_assessment_course'),
            models.Index(fields=['teacher_id'], name='idx_assessment_teacher'),
            models.Index(fields=['semester'], name='idx_assessment_semester'),
        ]
        
    def __str__(self):
        return f'评估{self.assessment_id}'


class AssessmentDetail(models.Model):
    """
    评估详情表
    @description 记录教学质量评估的详细指标得分
    @created 2024-04-08
    """
    id = models.AutoField(primary_key=True)
    assessment_id = models.IntegerField(null=True, blank=True, verbose_name='评估ID')
    indicator_id = models.IntegerField(null=True, blank=True, verbose_name='指标ID')
    score = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True, verbose_name='评分')
    
    class Meta:
        db_table = 'assessment_details'  # 指定表名
        verbose_name = '评估详情'
        verbose_name_plural = '评估详情'
        indexes = [
            models.Index(fields=['assessment_id'], name='idx_detail_assessment'),
            models.Index(fields=['indicator_id'], name='idx_detail_indicator'),
        ]
        
    def __str__(self):
        return f'详情{self.id}'


class QualityImprovementSuggestion(models.Model):
    """
    改进建议表
    @description 存储针对教学质量评估的改进建议
    @created 2024-04-08
    """
    suggestion_id = models.AutoField(primary_key=True, verbose_name='建议ID')
    assessment_id = models.IntegerField(null=True, blank=True, verbose_name='评估ID')
    title = models.CharField(max_length=100, null=True, blank=True, verbose_name='建议标题')
    content = models.TextField(null=True, blank=True, verbose_name='建议内容')
    priority = models.CharField(max_length=20, null=True, blank=True, verbose_name='优先级')
    target = models.CharField(max_length=50, null=True, blank=True, verbose_name='适用对象')
    is_adopted = models.SmallIntegerField(null=True, blank=True, default=0, verbose_name='是否已采纳')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        db_table = 'quality_improvement_suggestions'  # 指定表名
        verbose_name = '质量改进建议'
        verbose_name_plural = '质量改进建议'
        indexes = [
            models.Index(fields=['assessment_id'], name='idx_suggestion_assessment'),
            models.Index(fields=['priority'], name='idx_suggestion_priority'),
            models.Index(fields=['target'], name='idx_suggestion_target'),
            models.Index(fields=['is_adopted'], name='idx_suggestion_adopted'),
        ]
        
    def __str__(self):
        return self.title


# 四、教师发展支持系统相关表

class TeachingStyle(models.Model):
    """
    教学风格分析表
    @description 存储教师的教学风格分析数据
    @created 2024-04-08
    """
    id = models.AutoField(primary_key=True)
    teacher_id = models.IntegerField(null=True, blank=True, verbose_name='教师ID')
    style_data = models.TextField(null=True, blank=True, verbose_name='风格数据(JSON)')
    style_summary = models.TextField(null=True, blank=True, verbose_name='风格概述')
    analyzed_at = models.DateTimeField(null=True, blank=True, verbose_name='分析时间')
    
    class Meta:
        db_table = 'teaching_styles'  # 指定表名
        verbose_name = '教学风格分析'
        verbose_name_plural = '教学风格分析'
        indexes = [
            models.Index(fields=['teacher_id'], name='idx_style_teacher'),
            models.Index(fields=['analyzed_at'], name='idx_style_time'),
        ]
        
    def __str__(self):
        return f'教师{self.teacher_id}风格分析'


class TrainingRecommendation(models.Model):
    """
    教师培训推荐表
    @description 存储系统为教师推荐的培训
    @created 2024-04-08
    """
    id = models.AutoField(primary_key=True)
    teacher_id = models.IntegerField(null=True, blank=True, verbose_name='教师ID')
    title = models.CharField(max_length=100, null=True, blank=True, verbose_name='培训标题')
    description = models.TextField(null=True, blank=True, verbose_name='培训描述')
    match_score = models.IntegerField(null=True, blank=True, verbose_name='匹配度(百分比)')
    duration = models.CharField(max_length=50, null=True, blank=True, verbose_name='培训时长')
    is_enrolled = models.SmallIntegerField(null=True, blank=True, default=0, verbose_name='是否已报名')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        db_table = 'training_recommendations'  # 指定表名
        verbose_name = '培训推荐'
        verbose_name_plural = '培训推荐'
        indexes = [
            models.Index(fields=['teacher_id'], name='idx_training_teacher'),
            models.Index(fields=['match_score'], name='idx_training_match'),
            models.Index(fields=['is_enrolled'], name='idx_training_enrolled'),
        ]
        
    def __str__(self):
        return self.title


class CollaborationRecommendation(models.Model):
    """
    教师协作推荐表
    @description 存储系统为教师推荐的协作对象
    @created 2024-04-08
    """
    id = models.AutoField(primary_key=True)
    teacher_id = models.IntegerField(null=True, blank=True, verbose_name='教师ID')
    target_teacher_id = models.IntegerField(null=True, blank=True, verbose_name='目标教师ID')
    match_score = models.IntegerField(null=True, blank=True, verbose_name='匹配度(百分比)')
    reason = models.TextField(null=True, blank=True, verbose_name='推荐理由')
    status = models.SmallIntegerField(null=True, blank=True, default=0, verbose_name='状态')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        db_table = 'collaboration_recommendations'  # 指定表名
        verbose_name = '协作推荐'
        verbose_name_plural = '协作推荐'
        indexes = [
            models.Index(fields=['teacher_id'], name='idx_collab_teacher'),
            models.Index(fields=['target_teacher_id'], name='idx_collab_target'),
            models.Index(fields=['match_score'], name='idx_collab_match'),
            models.Index(fields=['status'], name='idx_collab_status'),
        ]
        
    def __str__(self):
        return f'教师{self.teacher_id}-{self.target_teacher_id}协作推荐'


class ResourceRecommendation(models.Model):
    """
    教学资源推荐表
    @description 存储系统为教师推荐的教学资源
    @created 2024-04-08
    """
    id = models.AutoField(primary_key=True)
    teacher_id = models.IntegerField(null=True, blank=True, verbose_name='教师ID')
    resource_id = models.IntegerField(null=True, blank=True, verbose_name='资源ID')
    external_title = models.CharField(max_length=100, null=True, blank=True, verbose_name='外部资源标题')
    external_url = models.CharField(max_length=255, null=True, blank=True, verbose_name='外部资源URL')
    external_type = models.CharField(max_length=50, null=True, blank=True, verbose_name='外部资源类型')
    category = models.CharField(max_length=50, null=True, blank=True, verbose_name='资源分类')
    relevance_score = models.IntegerField(null=True, blank=True, verbose_name='相关度(百分比)')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        db_table = 'resource_recommendations'  # 指定表名
        verbose_name = '资源推荐'
        verbose_name_plural = '资源推荐'
        indexes = [
            models.Index(fields=['teacher_id'], name='idx_res_rec_teacher'),
            models.Index(fields=['resource_id'], name='idx_res_rec_resource'),
            models.Index(fields=['category'], name='idx_res_rec_category'),
            models.Index(fields=['relevance_score'], name='idx_res_rec_relevance'),
        ]
        
    def __str__(self):
        return self.external_title


# 五、智能交互式帮助系统相关表

class FaqItem(models.Model):
    """
    常见问题表
    @description 存储系统常见问题及答案
    @created 2024-04-08
    """
    faq_id = models.AutoField(primary_key=True, verbose_name='问题ID')
    question = models.CharField(max_length=200, null=True, blank=True, verbose_name='问题')
    answer = models.TextField(null=True, blank=True, verbose_name='回答')
    category = models.CharField(max_length=50, null=True, blank=True, verbose_name='分类')
    view_count = models.IntegerField(null=True, blank=True, default=0, verbose_name='查看次数')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'faq_items'  # 指定表名
        verbose_name = '常见问题'
        verbose_name_plural = '常见问题'
        indexes = [
            models.Index(fields=['category'], name='idx_faq_category'),
            models.Index(fields=['view_count'], name='idx_faq_views'),
        ]
        
    def __str__(self):
        return self.question


class SystemGuide(models.Model):
    """
    系统操作指南表
    @description 存储系统操作指南
    @created 2024-04-08
    """
    guide_id = models.AutoField(primary_key=True, verbose_name='指南ID')
    title = models.CharField(max_length=100, null=True, blank=True, verbose_name='指南标题')
    content = models.TextField(null=True, blank=True, verbose_name='指南内容')
    module = models.CharField(max_length=50, null=True, blank=True, verbose_name='相关模块')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'system_guides'  # 指定表名
        verbose_name = '系统指南'
        verbose_name_plural = '系统指南'
        indexes = [
            models.Index(fields=['module'], name='idx_guide_module'),
        ]
        
    def __str__(self):
        return self.title


class HelpSession(models.Model):
    """
    用户帮助会话表
    @description 记录用户的帮助会话
    @created 2024-04-08
    """
    session_id = models.AutoField(primary_key=True, verbose_name='会话ID')
    user_id = models.IntegerField(null=True, blank=True, verbose_name='用户ID')
    start_time = models.DateTimeField(null=True, blank=True, verbose_name='开始时间')
    end_time = models.DateTimeField(null=True, blank=True, verbose_name='结束时间')
    feedback_rating = models.SmallIntegerField(null=True, blank=True, verbose_name='反馈评分')
    feedback_comment = models.TextField(null=True, blank=True, verbose_name='反馈评论')
    
    class Meta:
        db_table = 'help_sessions'  # 指定表名
        verbose_name = '帮助会话'
        verbose_name_plural = '帮助会话'
        indexes = [
            models.Index(fields=['user_id'], name='idx_session_user'),
            models.Index(fields=['start_time'], name='idx_session_start'),
            models.Index(fields=['feedback_rating'], name='idx_session_rating'),
        ]
        
    def __str__(self):
        return f'会话{self.session_id}'


class HelpMessage(models.Model):
    """
    帮助会话消息表
    @description 记录帮助会话中的消息
    @created 2024-04-08
    """
    message_id = models.AutoField(primary_key=True, verbose_name='消息ID')
    session_id = models.IntegerField(null=True, blank=True, verbose_name='会话ID')
    is_from_user = models.SmallIntegerField(null=True, blank=True, verbose_name='是否用户发送')
    message_text = models.TextField(null=True, blank=True, verbose_name='消息内容')
    sent_at = models.DateTimeField(null=True, blank=True, verbose_name='发送时间')
    
    class Meta:
        db_table = 'help_messages'  # 指定表名
        verbose_name = '帮助消息'
        verbose_name_plural = '帮助消息'
        indexes = [
            models.Index(fields=['session_id'], name='idx_message_session'),
            models.Index(fields=['sent_at'], name='idx_message_time'),
        ]
        
    def __str__(self):
        return f'消息{self.message_id}'


# 六、系统功能与分析相关表

class MenuPermission(models.Model):
    """
    菜单权限表
    @description 存储系统菜单及权限配置
    @created 2024-04-08
    """
    menu_id = models.AutoField(primary_key=True, verbose_name='菜单ID')
    parent_id = models.IntegerField(null=True, blank=True, verbose_name='父菜单ID')
    menu_name = models.CharField(max_length=50, null=True, blank=True, verbose_name='菜单名称')
    menu_url = models.CharField(max_length=200, null=True, blank=True, verbose_name='菜单URL')
    menu_icon = models.CharField(max_length=50, null=True, blank=True, verbose_name='菜单图标')
    permission_code = models.CharField(max_length=100, null=True, blank=True, verbose_name='权限标识')
    sort_order = models.IntegerField(null=True, blank=True, verbose_name='排序顺序')
    is_visible = models.SmallIntegerField(null=True, blank=True, default=1, verbose_name='是否可见')
    
    class Meta:
        db_table = 'menu_permissions'  # 指定表名
        verbose_name = '菜单权限'
        verbose_name_plural = '菜单权限'
        indexes = [
            models.Index(fields=['parent_id'], name='idx_menu_parent'),
            models.Index(fields=['sort_order'], name='idx_menu_sort'),
            models.Index(fields=['is_visible'], name='idx_menu_visible'),
        ]
        
    def __str__(self):
        return self.menu_name


class RoleMenu(models.Model):
    """
    角色-菜单关联表
    @description 关联角色与菜单的多对多关系
    @created 2024-04-08
    """
    id = models.AutoField(primary_key=True)
    role = models.CharField(max_length=20, null=True, blank=True, verbose_name='角色')
    menu_id = models.IntegerField(null=True, blank=True, verbose_name='菜单ID')
    
    class Meta:
        db_table = 'role_menus'  # 指定表名
        verbose_name = '角色菜单关联'
        verbose_name_plural = '角色菜单关联'
        unique_together = ('role', 'menu_id')  # 组合唯一约束
        indexes = [
            models.Index(fields=['role'], name='idx_role_menu_role'),
            models.Index(fields=['menu_id'], name='idx_role_menu_menu'),
        ]
        
    def __str__(self):
        return f'{self.role}-菜单{self.menu_id}'


# 用户表，用于登录认证
class Users(models.Model):
    """
    用户表
    @description 存储系统用户账号信息，用于身份认证和权限管理
    @created 2024-05-19
    """
    user_id = models.AutoField(primary_key=True, verbose_name='用户ID')
    username = models.CharField(max_length=50, unique=True, null=True, blank=True, verbose_name='用户名')
    password = models.CharField(max_length=128, null=True, blank=True, verbose_name='密码')
    email = models.CharField(max_length=100, null=True, blank=True, verbose_name='邮箱')
    role = models.CharField(max_length=20, null=True, blank=True, verbose_name='角色')
    last_login = models.DateTimeField(null=True, blank=True, verbose_name='最后登录时间')
    status = models.SmallIntegerField(null=True, blank=True, default=1, verbose_name='状态')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'users'  # 指定表名
        verbose_name = '用户'
        verbose_name_plural = '用户'
        indexes = [
            models.Index(fields=['username'], name='idx_users_username'),
            models.Index(fields=['role'], name='idx_users_role'),
        ]
        
    def __str__(self):
        return self.username


class AttendanceRecord(models.Model):
    """
    出勤记录表
    @description 记录学生的出勤情况
    @created 2024-06-15
    """
    attendance_id = models.AutoField(primary_key=True, verbose_name='出勤记录ID')
    student_id = models.IntegerField(null=True, blank=True, verbose_name='学生ID')
    course_id = models.IntegerField(null=True, blank=True, verbose_name='课程ID')
    class_date = models.DateField(null=True, blank=True, verbose_name='上课日期')
    status = models.SmallIntegerField(null=True, blank=True, default=1, verbose_name='出勤状态') # 1-出勤, 0-缺勤, 2-请假
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        db_table = 'attendance_records'  # 指定表名
        verbose_name = '出勤记录'
        verbose_name_plural = '出勤记录'
        indexes = [
            models.Index(fields=['student_id'], name='idx_attendance_student'),
            models.Index(fields=['course_id'], name='idx_attendance_course'),
            models.Index(fields=['class_date'], name='idx_attendance_date'),
        ]
        
    def __str__(self):
        return f'学生{self.student_id}在{self.class_date}的出勤记录'


# 用户与学院关联表
class UserCollege(models.Model):
    """
    用户与学院关联表
    @description 存储院级和班级管理员与所属学院的关联关系
    @created 2024-06-10
    """
    id = models.AutoField(primary_key=True, verbose_name='ID')
    user_id = models.IntegerField(null=False, verbose_name='用户ID')
    college_id = models.IntegerField(null=False, verbose_name='学院ID')
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        db_table = 'user_college'  # 指定表名
        verbose_name = '用户学院关联'
        verbose_name_plural = '用户学院关联'
        indexes = [
            models.Index(fields=['user_id'], name='idx_user_college_user_id'),
            models.Index(fields=['college_id'], name='idx_user_college_college_id'),
        ]
        unique_together = ('user_id', 'college_id')  # 确保用户和学院的组合是唯一的
        
    def __str__(self):
        return f'用户{self.user_id}关联学院{self.college_id}'
